from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *

from controller.TodoController import TodoController
from view.LineCheckBox import LineCheckBoxHLayout


class TabView(QWidget):
    def __init__(self, value=None):
        super().__init__()
        self.date = value
        self.mainLayout = QVBoxLayout()
        self.mainLayout.setAlignment(Qt.AlignTop)
        self.setUI()
        self.todoController = TodoController()

    def getQlineEdit(self):
        editorWidget = QWidget()
        qeditLayout = QHBoxLayout()
        self.qlineEdit = QLineEdit()
        self.qlineEdit.setMaximumWidth(320)
        self.qlineEdit.setMinimumWidth(320)
        self.qlineEdit.setMaximumHeight(30)
        self.qlineEdit.setStyleSheet("letter-spacing:1px")
        self.qlineEdit.returnPressed.connect(self.qlineEnter)
        # self.qlineEdit.setStyleSheet('font-family: "微软雅黑","Times New Roman", Times, serif;font-size: 14px;')
        self.qlineEdit.setStyleSheet('padding: 5px; border: none;border-bottom: 1px solid #D3D3D3;font-family: "微软雅黑"')
        qeditLayout.addWidget(self.qlineEdit, alignment=Qt.AlignCenter)
        editorWidget.setLayout(qeditLayout)
        return editorWidget

    def getScrollAreaPanel(self):
        self.contentWidget = QWidget(flags=Qt.Widget)

        self.contentLayout = QVBoxLayout()
        self.contentLayout.setAlignment(Qt.AlignTop)
        self.contentWidget.setLayout(self.contentLayout)

        scrollPanel = QScrollArea()
        scrollPanel.setWidgetResizable(True)
        scrollPanel.setMinimumWidth(360)
        scrollPanel.setMaximumWidth(360)
        scrollPanel.setMinimumHeight(340)
        scrollPanel.setMaximumHeight(340)
        scrollPanel.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        scrollPanel.setStyleSheet('''
            QScrollArea {
                border:none;
            }
            
            QScrollBar:vertical {
                border-radius:10px;
                padding-top:14px;
                padding-bottom:14px;
                background: #ffffff;
                width: 8px;
                display: none;
            }
            
            QScrollBar::handle:vertical {
                background:#D3D3D3;
                border-radius:10px;
            }

            QScrollBar:vertical::up-arrow, QScrollBar:vertical::down-arrow {
                border: none;
            }
        ''')
        scrollPanel.setWidget(self.contentWidget)
        return scrollPanel

    def setUI(self):
        editorWidget = self.getQlineEdit()
        scrollPanel = self.getScrollAreaPanel()

        self.mainLayout.addWidget(editorWidget, alignment=Qt.AlignTop)
        self.mainLayout.addWidget(scrollPanel, alignment=Qt.AlignTop)
        self.setLayout(self.mainLayout)

    def qlineEnter(self):
        content = self.qlineEdit.text()
        print(len(content))
        if len(content) > 0:
            print('进入')
            self.todoController.addContentoToDb(self.date, content)
            todoItem = LineCheckBoxHLayout(content)
            self.qlineEdit.setText("")
            self.contentLayout.addWidget(todoItem, alignment=Qt.AlignCenter)
